import { ref, onMounted, onUnmounted } from "vue";

function useMouse() {
  const x = ref(0);
  const y = ref(0);
  function update(event: MouseEvent) {
    x.value = event.pageX;
    y.value = event.pageY;
  }

  // 在组件挂载时添加事件监听器
  onMounted(() => window.addEventListener("mousemove", update));

  // 在组件卸载时移除事件监听器
  onUnmounted(() => window.removeEventListener("mousemove", update));

  return { x, y };
}

export default useMouse;
